package api;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import models.Content;
import models.Photo;
import org.junit.Test;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import controllers.Data;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.Http.StatusCode;
import util.JsonParserHelper;
import base.BaseTest;

public class InternalSharesPhoto extends BaseTest {
	
	private String from = "eva.perez";
	
	@Test
	public void sharePhoto() throws Exception {
		//La interna eva perez se loguea en el sistema
		Map<String, String> loginUserParams = new HashMap<String, String>();
		loginUserParams.put("username", from+"@B");
		loginUserParams.put("password", "secret1");
		// Login here so the following request will be authenticated:
		Response response = POST("/login", loginUserParams);
		assertStatus(StatusCode.FOUND, response);
		cookies.putAll(response.cookies);
		
		//Comprobacion de que eva perez todavía no ha subida ninguna imagen a batzen.
		Request request = newRequest();
		request.cookies.putAll(cookies);
		response = GET(request, "/api/me/photos");
		assertStatus(StatusCode.OK, response);
		Data<Photo> photos = JsonParserHelper.toPhotoCollection(response.out.toByteArray());
		assertTrue("Eva perez no ha subido ninguna foto", photos.data.size()==0);
		
		//Eva perez sube una imagen a batzen.
		Map<String, String> parameters = new HashMap<String, String>();
		Map<java.lang.String, java.io.File> files = new HashMap<java.lang.String, java.io.File>();
		File image = locateFile("buscador.png");
		assertNotNull("El fichero buscador.png especificado no existe", image);
		files.put("data", image);
		request = newRequest();
		request.cookies.putAll(cookies);
		response =  POST(request, "/api/me/photos", parameters, files);
		String location = response.getHeader("location");
		assertNotNull(location);
		Photo photo = JsonParserHelper.toPhoto(response.out.toByteArray());
		assertTrue("El propietario de la foto tiene que ser Eva perez.", photo.owner.username.equals(from));
		
		//Se verifica que Juan Gomez a subida una imagen a batzen
		request = newRequest();
		request.cookies.putAll(cookies);
		response = GET(request, "/api/me/photos");
		assertStatus(StatusCode.OK, response);
		photos = JsonParserHelper.toPhotoCollection(response.out.toByteArray());
		assertTrue("Eva Perez tiene una imagen subida.", photos.data.size()==1);
		
		//Recupero la lista de familiares de la interna
		request = newRequest();
		request.cookies.putAll(cookies);
		response = GET(request, "/api/me/familiars");
		assertStatus(StatusCode.OK, response);
		JsonObject familiars = JsonParserHelper.toJsonObject(response.out.toByteArray());
		assertTrue("Eva Perez tiene dos familiares.", familiars.get("total").getAsInt()==2);

		//Eva perez comparte una foto con uno de sus familiares.
		JsonObject familiar = familiars.get("data").getAsJsonArray().get(0).getAsJsonObject();		
		JsonObject message = new JsonObject();
		message.addProperty("body", "Cuerpo del mensaje");
		JsonArray tos = new JsonArray();
		JsonObject to = new JsonObject();
		to.addProperty("id", familiar.get("id").getAsLong());
		to.addProperty("firstName", familiar.get("firstName").getAsString());
		tos.add(to);
		message.add("receivers", tos);
		JsonArray contents = new JsonArray();
		JsonObject content = new JsonObject();
		content.addProperty("type", Content.ContentType.PHOTO.getName());
		content.addProperty("id", photo.id);
		content.addProperty("source", location);
		contents.add(content);
		message.add("contents", contents);
		request = newRequest();
		request.cookies.putAll(cookies);
		response = POST(request,"/api/me/outbox", "application/json", message.toString());
		assertStatus(StatusCode.OK,response);
	}
	
}